<?php
/**
 * 我的工单
**/
include_once '../includes/common.php';
if($islogin2==1){}else sysmsg("您还未登录，请先登录",2,'./login.php',true);
$title='我的工单';
include_once './header.php';
?>
<style>
.gdan_gout{width:100%;height:auto;background-color:#fff;padding-bottom:1em}
.gdan_txt{height:3em;line-height:3em;text-indent:1em;font-family:"微软雅黑";font-weight:800;}
.gdan_txt>span{position:absolute;right:3em;}
.gdan_zhugan{width:96%;height:auto;padding-top:1em;margin-left:2%;padding-left:.5em;padding-right:1em;margin-bottom:1em;border-top:dashed 1px #a9a9a9}
.gdan_kjia1{width:auto;margin-left:4em;margin-top:-3em}
.gdan_xiaozhi{width:100%;height:1em;color:#a9a9a9;margin-bottom:1em}
.gdan_xiaozhi>span{position:absolute;right:3em;}
.gdan_huifu{width:100%;height:auto;margin-top:1em;border-top:solid #ccc 1px}
.gdan_srk{width:98%;height:8em;margin-left:1%;margin-top:1em;border-color:#6495ed}
.gdan_huifu1{width:6em;height:2.5em;border:none;background-color:#1e90ff;color:#fff;margin:.5em 0 .5em 1%}
.gdan_jied{width:100%;height:3em;line-height:3em;text-align:center;color:#129DDE}
.image img {width: 120px;height: 120px;margin: 0.3em;box-shadow: 3px 3px 18px 1px #ccc}
</style>
<?php
function display_type($type){
    global $conf;
    $types = explode('|', $conf['workorder_type']);
    if($type==0 || !array_key_exists($type-1,$types))
        return '其它问题';
    else
        return $types[$type-1];
}

function display_status($status){
    if($status==1)
        return '<font color="red">待补充</font>';
    elseif($status==2)
        return '<font color="green">已结单</font>';
    else
        return '<font color="blue">待处理</font>';
}

$count1=$DB->count("SELECT count(*) FROM authguao_workorder WHERE uid='{$userrow['uid']}' AND status=1");
$count2=$DB->count("SELECT count(*) FROM authguao_workorder WHERE uid='{$userrow['uid']}' AND status=0");
$count3=$DB->count("SELECT count(*) FROM authguao_workorder WHERE uid='{$userrow['uid']}'");

$my=isset($_GET['my'])?$_GET['my']:null;
if($my=='add'){
?>
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                提交工单
            </div>
            <div class="card-body">
                <form class="form-horizontal layui-form">
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">问题类型：</label>
                        <select name="type" class="form-control" lay-search lay-filter="type">
                        <?php
                        foreach(explode('|', $conf['workorder_type']) as $key=>$value){
                            echo '<option value="'.($key+1).'">'.$value.'</option>';
                        }
                        ?>
                        <option value="0">其它问题</option>
                        </select>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">描述信息：</label>
                        <textarea class="form-control" name="content" rows="5" placeholder="填写描述信息" lay-verType="tips" lay-verify="required"></textarea>
                    </div>
                    <?php if($conf['workorder_pic']==1){?>
                    <div class="form-group mb-3">
                        <input type="file" id="file" onchange="fileUpload()" style="display:none;"/>
                        <label for="example-input-normal" style="font-weight: 500">问题图片：<a href="javascript:fileSelect()" class="badge badge-danger-lighten">上传图片</a></label>
                        <input type="text" class="form-control" id="picurl" name="picurl">
                    </div>
                    <div class="form-group mb-3" id="iamge_le" style="display: none;">
                        <div class="image"></div>
                    </div>
                    <?php }?>
                    <button type="submit" class="btn btn-block btn-xs btn-outline-success" lay-submit lay-filter="submit_addworkorder">提 交</button>
                </form>
            </div>
        </div>
    </div>
</div>
<?php
}elseif($my=='view'){
$id=intval($_GET['id']);
$rows=$DB->get_row("SELECT * FROM authguao_workorder WHERE id='$id' AND uid='{$userrow['uid']}' LIMIT 1");
if(!$rows)
    showmsg('警告','当前记录不存在！',2,'./');
$contents = explode('*',$rows['content']);
$myimg = $userrow['qq']?'//q2.qlogo.cn/headimg_dl?bs=qq&dst_uin='.$userrow['qq'].'&src_uin='.$userrow['qq'].'&fid='.$userrow['qq'].'&spec=640&url_enc=0&referer=bu_interface&term_type=PC':'../assets/img/user.png';
$kfimg = 'https://imgcache.qq.com/open_proj/proj_qcloud_v2/mc_2014/work-order/css/img/custom-service-avatar.svg';
?>
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                工单详情
            </div>
            <div class="gdan_gout">
                <div class="gdan_txt">沟通记录 - <?php echo count($contents)?><span>状态：<?php echo display_status($rows['status'])?></span></div>
                <!------------------开始沟通------------------------>
                <div class="gdan_zhugan" style="border: none;">
                    <img src="<?php echo $myimg?>" class="img-circle" width="40"/>
                    <div class="gdan_kjia1">
                        <div class="gdan_xiaozhi">问题描述<span><?php echo $rows['addtime']?></span></div>
                        <p><?php echo $contents[0]?></p><br/>
                        <p>问题类型：<?php echo display_type($rows['type'])?></p>
                        <?php echo $rows['picurl']?'<p>问题图片：[<span onclick="image_msg('.$id.')">点此查看</span>]':null;?>
                    </div>
                </div>
                <?php
                for($i=1;$i<count($contents);$i++){
                    $content = explode('^',$contents[$i]);
                    if(count($content)==3){
                        echo '<div class="gdan_zhugan"><img src="'.($content[0]==1?$kfimg:$myimg).'" class="img-circle" width="40"/><div class="gdan_kjia1"><div class="gdan_xiaozhi">'.($content[0]==1?'官方客服':$userrow['user']).'<span>'.$content[1].'</span></div>'.$content[2].'</div></div>';
                    }
                }
                if($rows['status']==0){?>
                <div class="gdan_jied">请耐心等待客服处理</div>
                <?php }elseif($rows['status']==2){?>
                <div class="gdan_jied">此工单已经结单</div>
                <?php }elseif($rows['status']==1){?>
                <div class="gdan_huifu">
                    <form onsubmit="return replyworkorder();" method="post" class="layui-form">
                    <textarea class="gdan_srk" name="content" placeholder="可输入需要补充的内容，回复后官方客服将会收到你的消息！" lay-verType="tips" lay-verify="required"></textarea>
                    <input type="submit" name="submit" value="提交回复" class="btn btn-outline-info">
                    <span class="btn btn-outline-success" onclick="complete_workorder();">完结工单</span>
                    </form>
                <?php }?>
                </div>
            </div>
        </div>
    </div>
</div>
<?php }else{?>
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                我的工单
                <span class="badge badge-primary-lighten">待我处理(<?php echo $count1?>)</span>
                <span class="badge badge-warning-lighten">处理中(<?php echo $count2?>) </span>
                <span class="badge badge-success-lighten">全部工单(<?php echo $count3?>)</span>
            </div>
            <div class="card-body">
                <a href="./workorder.php?my=add" class="btn btn-outline-primary"><i class="fa fa-plus"></i>&nbsp;提交工单</a>
                <div style="white-space:nowrap;overflow-x: auto;">
                    <table class="layui-table layuiadmin-page-table">
                        <thead><tr><th>ID</th><th>类型</th><th>问题描述</th><th>状态</th><th>提交时间</th><th>操作</th></tr></thead>
                        <tbody>
                        <?php
                        $numrows=$DB->count("SELECT count(*) from authguao_workorder WHERE uid='{$userrow['uid']}'");
                        $pagesize=30;
                        $pages=intval($numrows/$pagesize);
                        if ($numrows%$pagesize){
                            $pages++;
                        }
                        if (isset($_GET['page'])){
                            $page=intval($_GET['page']);
                        }else{
                            $page=1;
                        }
                        $offset=$pagesize*($page - 1);
                        $rs=$DB->query("SELECT * FROM authguao_workorder WHERE uid='{$userrow['uid']}' ORDER BY id DESC LIMIT $offset,$pagesize");
                        while($res = $DB->fetch($rs))
                        {
                        $content=explode('*',$res['content']);
                        $content=mb_substr($content[0], 0, 16, 'utf-8');
                        echo '<tr><td><b>'.$res['id'].'</b></td><td>'.display_type($res['type']).'</td><td><a href="./workorder.php?my=view&id='.$res['id'].'">'.htmlspecialchars($content).'</a></td><td>'.display_status($res['status']).'</td><td>'.$res['addtime'].'</td><td><a href="./workorder.php?my=view&id='.$res['id'].'" class="layui-btn layui-btn-xs btn-info">查看</a><span class="layui-btn layui-btn-xs btn-danger" onclick="delete('.$res['id'].')">删除</span></td></tr>';
                        }
                        ?>
                        </tbody>
                    </table>
                </div>
                <div class="text-center">
                <?php
                #分页
                $pageList=new Page($numrows,$pagesize,0,$link);
                echo $pageList->showPage();
                ?>
                </div>
            </div>
        </div>
    </div>
</div>
<?php }?>
<?php
include_once './bottom.php';
?>
<script type="text/javascript">
layui.use(['form'], function () {
    var form = layui.form;
    form.on('submit(submit_addworkorder)', function (data) {
        layer.alert('是否要执行当前操作？', {
            icon: 3, btn: ['确定', '取消'], btn1: function (layero, index) {
                addworkorder();
            }
        });
        return false;
    });
});
function image_fenltt(image) {
    var image_arr = image.split(",");
    var content = "";
    for (a in image_arr) {
        content += '<img layer-pid="' + a + '" alt="' + a + '" layer-src="' + image_arr[a] + '" src="' + image_arr[a] + '" />';
    }
    $(".image").html(content);
}
$('input[name="picurl"]').bind('input propertychange', function () {
    var image_log = $('input[name="picurl"]').val();
    if (image_log != '') {
        $("#iamge_le").show(200);
        image_fenltt(image_log);
    } else {
        $("#iamge_le").hide(200);
    }
});
function image_msg(id) {
    $.getJSON('../ajax.php?act=image_workorder&id=' + id, function (json) {
        layer.photos({photos: json, anim: 5});
    });
}
function fileSelect(){
    $("#file").trigger("click");
}
function fileUpload(){
    var fileObj = $("#file")[0].files[0];
    if (typeof (fileObj) == "undefined" || fileObj.size <= 0) {
        return;
    }
    var formData = new FormData();
    formData.append("do","upload");
    formData.append("file",fileObj);
    var ii = layer.msg('正在上传图片中,请稍后...', {icon: 16, time: 10 * 1000});
    $.ajax({
        url: "ajax.php?act=uploadimg",
        data: formData,
        type: "POST",
        dataType: "json",
        cache: false,
        processData: false,
        contentType: false,
        success: function (data) {
            layer.close(ii);
            if(data.code == 0){
                layer.msg('上传图片成功', {icon: 6});
                var picurl = $("input[name='picurl']").val();
                if(!picurl){
                    $("#picurl").val($("#picurl").val()+'../'+data.url);
                }else{
                    $("#picurl").val($("#picurl").val()+',../'+data.url);
                }
            }else{
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
    })
}
function addworkorder() {
    var type = $("select[name='type']").val();
    var content = $("textarea[name='content']").val();
    var picurl = $("input[name='picurl']").val();
    var ii = layer.msg('正在提交工单中,请稍后...', {icon: 16, time: 10 * 1000});
    $.ajax({
        type: "POST",
        url: "ajax.php?act=add_workorder",
        data : {type:type,content:content,picurl:picurl},
        dataType: "json",
        success: function(data) {
            layer.close(ii);
            if (data.code == 0) {
                layer.msg(data.msg, {
                    icon: 6,
                    end: function (layero, index) {
                        window.location.href = 'workorder.php';
                    }
                });
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
    });
    return false;
};
function replyworkorder() {
    var content = $("textarea[name='content']").val();
    var ii = layer.msg('正在提交回复中,请稍后...', {icon: 16, time: 10 * 1000});
    $.ajax({
        type: "POST",
        url: "ajax.php?act=reply_workorder&id=<?php echo $id;?>",
        data : {content:content},
        dataType: "json",
        success: function(data) {
            layer.close(ii);
            if (data.code == 0) {
                layer.msg(data.msg, {
                    icon: 6,
                    end: function (layero, index) {
                        window.location.reload();
                    }
                });
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
    });
    return false;
};
function complete_workorder() {
    var ii = layer.msg('正在完结工单中,请稍后...', {icon: 16, time: 10 * 1000});
    $.ajax({
        type: "GET",
        url: "ajax.php?act=complete_workorder&id=<?php echo $id;?>",
        dataType: "json",
        success: function(data) {
            layer.close(ii);
            if (data.code == 0) {
                layer.msg(data.msg, {
                    icon: 6,
                    end: function (layero, index) {
                        window.location.reload();
                    }
                });
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
    });
    return false;
};
</script>